// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,

    recommendShow: true,
    followShow: true,
    tvShow: true,

    swiperData: [],
    recommendData: [],
    followData: [],
    tvData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this

    this.getSwiperData(that)
    this.getRecommendData(that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  getSwiperData(that) {
    wx.request({
      url: 'https://apis.netstart.cn/douguo/home/events/getlists?offset=0limit=20',
      method: 'GET',
      success: function (res) {
        that.setData({
          swiperData: res.data.data.ongoing.events
        })
      }
    })
  },

  getRecommendData(that) {
    wx.request({
      url: 'https://apis.netstart.cn/douguo/home/recommended/20/10',
      method: 'GET',

      success: function (res) {
        const list = res.data.result.list
        const newList = []

        for (const item in list) {
          if (list[item].type == 1) {
            newList.push(list[item])
          }
        }

        that.setData({
          recommendData: newList
        })
      },

      complete() {
        that.setData({
          recommendShow: false
        })
      }

    })
  },

  getFollowData(that) {
    wx.request({
      url: 'https://apis.netstart.cn/douguo/home/ffeeds/0/20',
      method: 'GET',

      success: function (res) {
        const list = res.data.result.rfs
        const newList = []

        for (const item in list) {
          if (list[item].type == 1) {
            newList.push(list[item])
          }
        }

        that.setData({
          followData: newList
        })
      },

      complete() {
        that.setData({
          followShow: false
        })
      }

    })
  },

  getTvData(that) {
    wx.request({
      url: 'https://apis.netstart.cn/douguo/home/videos/0/20',
      method: 'GET',

      success: function (res) {
        const list = res.data.result.list
        const newList = []

        for (const item in list) {
          if (list[item].type == 1) {
            newList.push(list[item])
          }
        }

        that.setData({
          tvData: newList
        })
      },

      complete() {
        that.setData({
          tvShow: false
        })
      }

    })
  },

  onClick(event) {
    var that = this
    const title = event.detail.title

    if (title == '关注') {
      this.getFollowData(that)
    } else if (title == '视频') {
      this.getTvData(that)
    }
  },

  onClickJump(event) {
    const id = event.currentTarget.dataset.id

    wx.navigateTo({
      url: '../note/note?id=' + id
    })
  },

  onClickSearch() {
    wx.navigateTo({
      url: '../search/search'
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})